package com.hxzy.shop.marketing.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.hxzy.shop.common.dto.marketing.req.DistributionGlobalRuleUpdateReq;
import com.hxzy.shop.entity.DistributionBindRule;
import com.hxzy.shop.entity.DistributionGlobalRule;
import com.hxzy.shop.marketing.dto.resp.DistributionGlobalAndBindRuleResp;
import com.hxzy.shop.marketing.mapper.DistributionBindRuleMapper;
import com.hxzy.shop.marketing.service.DistributionGlobalRuleService;
import com.hxzy.shop.marketing.mapper.DistributionGlobalRuleMapper;
import com.hxzy.shop.vo.ApiException;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.Map;
import java.util.function.Function;

/**
* @author admin
* @description 针对表【distribution_global_rule】的数据库操作Service实现
* @createDate 2023-08-02 21:46:04
*/
@Service
public class DistributionGlobalRuleServiceImpl extends ServiceImpl<DistributionGlobalRuleMapper, DistributionGlobalRule>
    implements DistributionGlobalRuleService{

    @Override
    public boolean saveBatch(Collection<DistributionGlobalRule> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean saveOrUpdateBatch(Collection<DistributionGlobalRule> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean updateBatchById(Collection<DistributionGlobalRule> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean saveOrUpdate(DistributionGlobalRule entity) {
        return false;
    }

    @Override
    public DistributionGlobalRule getOne(Wrapper<DistributionGlobalRule> queryWrapper, boolean throwEx) {
        return null;
    }

    @Override
    public Map<String, Object> getMap(Wrapper<DistributionGlobalRule> queryWrapper) {
        return null;
    }

    @Override
    public <V> V getObj(Wrapper<DistributionGlobalRule> queryWrapper, Function<? super Object, V> mapper) {
        return null;
    }

    @Resource
    private DistributionGlobalRuleMapper distributionGlobalRuleMapper;

    @Resource
    private DistributionBindRuleMapper distributionBindRuleMapper;
    @Override
    public DistributionGlobalAndBindRuleResp selectGlobalRule() {
        DistributionGlobalAndBindRuleResp distributionGlobalAndBindRuleResp = new DistributionGlobalAndBindRuleResp();
        DistributionGlobalRule distributionGlobalRule = distributionGlobalRuleMapper.selectOne(null);
        DistributionBindRule distributionBindRule = distributionBindRuleMapper.selectOne(null);
        if(ObjectUtil.isNotEmpty(distributionBindRule)){
            distributionGlobalAndBindRuleResp.setBind(distributionBindRule.getBind());
            distributionGlobalAndBindRuleResp.setBindTime(distributionBindRule.getBindTime());
            distributionGlobalAndBindRuleResp.setReplaceBind(distributionBindRule.getReplaceBind());
        }
        if(ObjectUtil.isNotEmpty(distributionGlobalRule)){
            distributionGlobalAndBindRuleResp.setSettlementPoint(distributionGlobalRule.getSettlementPoint());
            distributionGlobalAndBindRuleResp.setSingleWithdrawLimt(distributionGlobalRule.getSingleWithdrawLimt());
            distributionGlobalAndBindRuleResp.setWithdrawLimit(distributionGlobalRule.getWithdrawLimit());
            distributionGlobalAndBindRuleResp.setMerchantId(distributionGlobalRule.getMerchantId());
        }
        return distributionGlobalAndBindRuleResp;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Object globalRuleUpdate(DistributionGlobalRuleUpdateReq req) {
        DistributionGlobalRule rule = this.distributionGlobalRuleMapper.selectOne(null);
        //id不存在，新增
        if (ObjectUtil.isEmpty(rule)) {
            rule = new DistributionGlobalRule();
            rule.setSettlementPoint(req.getSettlementPoint());
            if (ObjectUtil.isNotEmpty(req.getWithdrawLimit()) &&
            req.getWithdrawLimit().compareTo(new BigDecimal(100))<0){
                throw new ApiException(500,"体现门槛必须大于100");
            }
            rule.setWithdrawLimit(req.getWithdrawLimit());
            rule.setSingleWithdrawLimt(new BigDecimal(100));
            this.distributionGlobalRuleMapper.insert(rule);
        }else {
            //id存在，修改
            rule.setSettlementPoint(req.getSettlementPoint());
            if (ObjectUtil.isNotEmpty(req.getWithdrawLimit()) &&
                    req.getWithdrawLimit().compareTo(new BigDecimal(100))<0){
                throw new ApiException(500,"体现门槛必须大于100");
            }
            rule.setWithdrawLimit(req.getWithdrawLimit());
            this.distributionGlobalRuleMapper.updateById(rule);
        }
        //新增分销规则
        DistributionBindRule bindRule = this.distributionBindRuleMapper.selectOne(null);
        if (ObjectUtil.isEmpty(bindRule)){
            bindRule = new DistributionBindRule();
            if (ObjectUtil.isEmpty(req.getBindTime()) && req.getBind() ==2){
                throw new ApiException(500,"该选项必须选择绑定时间 且必须大于等于1");
            }
            if (req.getBind() == 1){
                bindRule.setBindTime(1);
            }else {
                bindRule.setBindTime(req.getBindTime());
            }
            bindRule.setBind(req.getBind());
            bindRule.setReplaceBind(req.getReplaceBind());
            distributionBindRuleMapper.insert(bindRule);
        }else {
            //修改
            if (ObjectUtil.isEmpty(req.getBindTime()) && req.getBind() ==2){
                throw new ApiException(500,"该选项必须选择绑定时间 且必须大于等于1");
            }
            if (req.getBind() == 1){
                bindRule.setBindTime(1);
            }else {
                bindRule.setBindTime(req.getBindTime());
            }
            bindRule.setBind(req.getBind());
            bindRule.setReplaceBind(req.getReplaceBind());
            this.distributionBindRuleMapper.updateById(bindRule);
        }
        return true;
    }
}




